//
//  JJHttpTransportRequest.swift
//  swiftStudy
//
//  Created by hamilyjing on 2020/6/26.
//  Copyright © 2020 JJ. All rights reserved.
//

import Foundation

class JJHttpTransportRequest: JJRequest {
    override init(identity: String?, parameters: [String: Any]?, isSaveToMemory: Bool, isSaveToDisk: Bool) {
        super.init(identity: identity, parameters: parameters, isSaveToMemory: isSaveToMemory, isSaveToDisk: isSaveToDisk)
        userCacheDirectory = "JJHttpTransportService"
    }
    
    override func host() -> String {
        let host = JJUrlConfigManager.shared.serverHost(identity!)
        return host
    }
    
    override func path() -> String {
        let path = JJUrlConfigManager.shared.serverPath(identity!)
        return path
    }
    
    override func parameters() -> [String : Any]? {
        return requestParameters
    }
    
    override func convertToModel() -> JJResponseBaseModelProtocol? {
        let responseDictionary = responseString?.jjs_dictionaryWithJSON()
        if responseDictionary == nil {
            return nil
        }
        
        let dataDictionary = getConvertModelContent(responseDictionary!)
        
        let appName = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        let modelClass = NSClassFromString(appName + "." + modelClassName) as! JJResponseBaseModel.Type
        let model: JJResponseBaseModel? = modelClass.init()
        switch dataDictionary {
        case let object as [String : Any]:
            model?.resultDictionary = object
        case let object as String:
            model?.resultString = object
        case let object as [Any]:
            model?.resultArray = object
        default:
            return nil
        }
        
        return model
    }
    
    func getConvertModelContent(_ content_: [AnyHashable: Any]) -> Any {
        return content_
    }
    
    override func requestFailFilter() {
        super.requestFailFilter()
        
        processAbnormalStatus()
    }
    
    func processAbnormalStatus() -> Void {
        // process login session timeout ...
    }
}
